//
//  CHLoading.swift
//  CHSwift
//
//  Created by cyptt on 2021/12/21.
//

import UIKit
import SnapKit
 public class CHLoading: UIView {

    //MARK: -lazy
    
    var defaltH = 120
    var defaltW = 120
    
    var loadingColor:UIColor = UIColor.white
    var contentColor:UIColor = UIColor.init(_colorLiteralRed: 83/255.0, green: 83/255.0, blue: 83/255.0, alpha: 1)
    var fontSize:CGFloat = 15
     
     var contentWConstraint:Constraint?
     var contentHConstraint:Constraint?
     var parentContentView:UIView?
     private var sizeH:CGFloat = 20
    override init(frame: CGRect) {
        super.init(frame: frame)
    

        
    }
    
    public init(){
         super.init(frame: CGRect.zero)
         let window:UIWindow = keyWindow
         self.parentContentView = window
        self.setUI()
     }
    
     public init(contentView:UIView){
        super.init(frame: CGRect.zero)
        self.parentContentView = contentView
         self.setUI()
     }
     
     func setUI(){
         
     }
    
     required init?(coder: NSCoder) {
         fatalError("init(coder:) has not been implemented")
     }
     
//    required init?(coder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
//    }
    
    lazy var mskView = {()-> UIView in

        let view:UIView = UIView.init(frame: self.parentContentView!.bounds)
        view.backgroundColor = UIColor.init(_colorLiteralRed: 0, green: 0, blue: 0, alpha: 0.00)
    
        self.parentContentView!.addSubview(view)
        return view
    }()
    
    
  
    lazy var contentV = {()-> UIView in
        let view:UIView = UIView.init()
        view.backgroundColor = self.contentColor
        view.layer.cornerRadius = 8
        view.layer.masksToBounds = true
        view.addSubview(self.spinner)
        view.addSubview(self.msgLable)
        return view
    }()
    
    lazy var spinner = {()-> UIActivityIndicatorView in
        let spinner:UIActivityIndicatorView = UIActivityIndicatorView.init(style: UIActivityIndicatorView.Style.white)
        spinner.color = self.loadingColor
        return spinner
    }()
    
    lazy var msgLable = {()-> UILabel in
        let lable:UILabel = UILabel.init()
        lable.textColor = self.loadingColor
        lable.font = UIFont.systemFont(ofSize: self.fontSize)
        lable.numberOfLines = 0
        lable.textAlignment = NSTextAlignment.center
        
       
   
        return lable
    }()
    
    let keyWindow:UIWindow = {
        var window:UIWindow
        if #available(iOS 13.0, *) {
            window = UIApplication.shared.windows.filter({ $0.isKeyWindow }).last!
        }else {
            window = UIApplication.shared.keyWindow!
        }
        return window
    }()
    
    
     public func show(msg:String,isNoLoading:Bool = false){
         if isNoLoading == false{
             let maxW = Int(self.parentContentView!.frame.size.width - 80)
             self.sizeH = self.labelHeight(width: CGFloat(maxW), fontSize: self.fontSize, text: msg)
             
             print(self.sizeH)
             self.parentContentView!.addSubview(self.mskView)
             self.mskView.addSubview(self.contentV)
             self.spinner.startAnimating()
             self.msgLable.text = msg
             setPosition(msg: msg)
         }else{
             self.parentContentView!.addSubview(self.mskView)
         }
   
    }
    
   
    
    
     public func hide(){
         self.mskView.removeFromSuperview()
         
     }
     public func hide(after:Double){
         
         DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + after) {
             self.mskView.removeFromSuperview()
         }
     }
     
     

}

//MARK: --setPosition
extension CHLoading{
 
    func setPosition(msg:String){

        var spinerMargin = 0
        if msg.count == 0 {
            spinerMargin = 40
        }else{
            spinerMargin = 40
        }
        let msgCount = msg.count
        let msgW = msgCount * Int(self.fontSize)
        
        let maxW = Int(self.parentContentView!.frame.size.width - 80)
        var contentW = defaltW //显示视图宽度
        var contentH = defaltH // 显示视图高度
        
        
        if msgW < defaltW {
            contentW = defaltW
        } else if msgW > maxW {
            //字数宽度大于最大宽度
            contentW = maxW
            print("msgW--",msgW)
            print("maxw",maxW)
            contentH = Int(self.sizeH) + defaltH
//            if msgW > 3 * maxW {
//                print("进来了")
//                //计算高度
//                print(msgW/maxW)
//                print((msgW/maxW) * Int(self.fontSize))
//                contentH = Int(self.sizeH) + defaltH
//            }
        }else {
            contentW = msgW
        }
        
        self.contentWConstraint?.update(offset: contentW)
        self.contentHConstraint?.update(offset: contentH)

        self.contentV.snp.remakeConstraints { make in
            make.center.equalTo(self.mskView)
            self.contentWConstraint =  make.width.equalTo(contentW).constraint
            self.contentHConstraint =  make.height.equalTo(contentH).constraint
        }
        self.spinner.snp.remakeConstraints { make in
            make.centerX.equalTo(self.mskView)
            make.top.equalTo(self.contentV).offset(spinerMargin)
        }
        self.msgLable.snp.remakeConstraints { make in

            make.left.equalTo(self.contentV).offset(5)
            make.right.equalTo(self.contentV.snp.right).offset(-5)
            make.top.equalTo(self.spinner.snp.bottom).offset(10)
            make.bottom.equalTo(self.contentV.snp.bottom)

        }
        
    }
    
    
    /*
     计算label高度传参数
     */
    public func labelHeight(width:CGFloat,fontSize:CGFloat,text:String)->CGFloat{
        let dic = [NSAttributedString.Key.font : UIFont.systemFont(ofSize: fontSize)]
        let size = CGSize(width: width, height: CGFloat(MAXFLOAT))
        let rect = text.boundingRect(with: size, options: [.usesFontLeading,.usesLineFragmentOrigin], attributes: dic, context: nil)
        return CGFloat(ceilf(Float(rect.size.height)))
    }
    
    
}






